package com.xinqi.modules.sys.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.dict.convert.DictTypeConvert;
import com.xinqi.modules.sys.dict.domain.DictTypeEntity;
import com.xinqi.modules.sys.dict.dto.req.DictTypeCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictTypeDeleteDTO;
import com.xinqi.modules.sys.dict.dto.req.DictTypeQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictTypeUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictTypeResultDTO;
import com.xinqi.modules.sys.dict.manager.DictTypeManager;
import com.xinqi.modules.sys.dict.service.DictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 字典类型表服务层实现
 *
 * @author: linkai
 * @date: 2023/05/21
 */
@Validated
@Service("dictTypeService")
@RequiredArgsConstructor
public class DictTypeServiceImpl implements DictTypeService {
    private final DictTypeConvert dictTypeConvert;
    private final DictTypeManager dictTypeManager;

    /**
     * 分页查询 字典类型表数据
     */
    @Override
    @DS("slaver")
    public PageInfo<DictTypeResultDTO> page(Param pageable, DictTypeQueryDTO query) {
        IPage<DictTypeEntity> page = dictTypeManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, dictTypeConvert::convert);
    }

    /**
     * 查询 字典类型表数据
     */
    @Override
    public List<DictTypeResultDTO> find(DictTypeQueryDTO query) {
        return dictTypeManager.list(wrapper(query)).stream()
            .map(dictTypeConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<DictTypeEntity> wrapper(DictTypeQueryDTO query) {
        LambdaQueryWrapper<DictTypeEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(DictTypeEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 字典类型表数据
     */
    @Override
    public DictTypeResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(dictTypeManager.findById(id)).map(dictTypeConvert::convert).orElse(null);
    }

    /**
     * 新增 字典类型表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid DictTypeCreateDTO dto) {
        DictTypeEntity entity = dictTypeConvert.create(dto);
        dictTypeManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 字典类型表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid DictTypeUpdateDTO dto) {
        Optional.ofNullable(dictTypeManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        DictTypeEntity entity = dictTypeConvert.update(dto);
        return dictTypeManager.updateById(entity);
    }

    /**
     * 根据id删除 字典类型表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return dictTypeManager.removeById(id);
    }

    /**
     * 根据id删除 字典类型表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(DictTypeDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
